package org.csu.mypetstore.controller;

import org.csu.mypetstore.domain.*;
import org.csu.mypetstore.service.CatalogService;
import org.csu.mypetstore.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.context.annotation.SessionScope;

import javax.servlet.http.HttpSession;
import javax.sound.sampled.Line;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@Controller
@SessionScope
@RequestMapping("/order")
@SessionAttributes({"order" , "cart" ,"account"})
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private Order order;
    @Autowired
    private CatalogService catalogService;

    private static final List<String> CARD_TYPE_LIST ;
    static {
        List<String> cardList = new ArrayList<String>();
        cardList.add("Visa");
        cardList.add("MasterCard");
        cardList.add("American Express");
        CARD_TYPE_LIST = Collections.unmodifiableList(cardList);
    }
    private boolean shippingAddressRequired;
    private boolean confirmed;
    private List<Order> orderList;

    public OrderService getOrderService() {
        return orderService;
    }

    public void setOrderService(OrderService orderService) {
        this.orderService = orderService;
    }

    public static List<String> getCardTypeList() {
        return CARD_TYPE_LIST;
    }

    public Order getOrder() {
        return order;
    }

    public void setOrder(Order order) {
        this.order = order;
    }

    public boolean isShippingAddressRequired() {
        return shippingAddressRequired;
    }

    public void setShippingAddressRequired(boolean shippingAddressRequired) {
        this.shippingAddressRequired = shippingAddressRequired;
    }

    public boolean isConfirmed() {
        return confirmed;
    }

    public void setConfirmed(boolean confirmed) {
        this.confirmed = confirmed;
    }

    public List<Order> getOrderList() {
        return orderList;
    }

    public void setOrderList(List<Order> orderList) {
        this.orderList = orderList;
    }

    @GetMapping("/newOrderForm")
    public String newOrderForm(@SessionAttribute("account") Account account,  Cart cart, Model model){
        for (int i = 0; i<cart.getCartItemList().size(); i++){
            CartItem item1 = cart.getCartItemList().get(i);
            Item item2 = catalogService.getItem(item1.getItem().getItemId());
            if (item1.getQuantity() > item2.getQuantity()){
                String msg = "库存小于当前购买数量，请减少数量";
                model.addAttribute("msg",msg);
                return "/cart/cart";
            }
        }
        if (account.getUsername() == null){
            String msg = "You must sign on before attempting to check out. Please sign on and try checking out again. ";
            model.addAttribute("msg",msg);
            return "/account/signonForm";
        }else if (cart != null){
            order.initOrder(account,cart);
            model.addAttribute("order",order);
            model.addAttribute("newOrder",new Order());
            return "order/newOrderForm";
        }else {
            String msg = "An order could not be created because a cart could not be found.";
            model.addAttribute("msg",msg);
            return "/catalog/view";
        }
    }

    @PostMapping("confirmorderaction")
    public String confirmOrderAction(boolean shippingAddressRequired,Order order,Model model){
        model.addAttribute("order",order);
        if (shippingAddressRequired){
            shippingAddressRequired = false;
            return "order/shippingForm";
        }else if(!isConfirmed()){
            return "order/confirmOrder";
        }else{
            String msg = "ERROR";
            return "common/error";
        }
    }

    @GetMapping("/viewOrder")
    public String viewOrder(Model model, Cart cart, HttpSession session, SessionStatus sessionStatus){
        Order order =(Order) model.getAttribute("order");
        orderService.insertOrder(order);
        return "order/viewOrder";
    }

    @GetMapping("/return")
    public String returnToMenu(Cart cart, HttpSession session, SessionStatus sessionStatus){
        order.getLineItems().clear();
        cart.getCartItemList().clear();
        return "catalog/main";
    }

    @GetMapping("/viewOrderList")
    public String viewOrderList(@SessionAttribute("account") Account account,Model model){

        List<Order> orderList = new ArrayList<>();
        orderList = orderService.getOrdersByUsername(account.getUsername());
        model.addAttribute("orderList",orderList);
        return "order/listOrders";
    }

    @GetMapping("/viewOrderByOrderId")
    public String viewOrderByOrderId(String orderId, Model model){
        int order_id = Integer.parseInt(orderId);
        order = orderService.getOrder(order_id);
        System.out.println("$$$$$$$$$$$@@");
        model.addAttribute("order",order);
        return "order/viewOrder";
    }
}
